package edu.byu.isys413.group1E;
import edu.byu.isys413.group1E.data.*;
import java.sql.SQLException;
import java.util.ArrayList;

public class PrintHandler {
    
    public void sendEmails() throws Exception {
        
        //Make DAOs for POL, TXLine, TX, Post Office
        PrintOrderDAO poDAO = (PrintOrderDAO)PrintOrderDAO.getInstance();
        TransLineDAO txlDAO = TransLineDAO.getInstance();
        TransactionDAO txDAO = TransactionDAO.getInstance();
        PostOffice po = new PostOffice();
        
        //Get list of ready Print Order Lines
        ArrayList<RevenueSourceBO> BOList;
        BOList = poDAO.getReady();
        
        //Initiate some Ints
        int index = 0;
        int max = BOList.size();
        
        //For Each List Item
        while (index < max) {
            
            //Yank out a poBO from the list
            PrintOrderBO poBO =  (PrintOrderBO)BOList.get(index);
            //get the email based on poBO
            String email = txDAO.read(txlDAO.getbyRSID(poBO.getRevSrcID()).getTransactionID()).getCustomer().getCustEmail();
            //Send the email
            po.SendReadyMessage(email);
            //Loop
            index++;
        }
        
        
        
        
    }
    
    public RevenueSourceBO addPrint(int qty, String descr, String PhotoSetID, String size, String printType, String photoType, double unitPrice) throws Exception {
        
        //Create TransactionLine
        TransLineDAO txDAO                =      TransLineDAO.getInstance();
        TransLineBO      txBO                =       txDAO.create();
        
        //Select PhotoSet
        PhotoSetDAO psDAO               =      new PhotoSetDAO();
        PhotoSetBO psBO                    =     (PhotoSetBO) psDAO.read(PhotoSetID);
        
        //Create New Photo
        PrintOrderDAO poDAO            =      new PrintOrderDAO();
        PrintOrderBO poBO                 =     (PrintOrderBO) poDAO.create();
        
        //Input Photo Type
        PrintFormatDAO ptDAO           =      new PrintFormatDAO();
        PrintFormatBO ptBO                 =      (PrintFormatBO)ptDAO.create();
        ptBO.setPhotoType(photoType);
        ptBO.setPrintType(printType);
        ptBO.setUnitPrice(unitPrice);
        ptBO.setXySize(size);
        ptDAO.save(ptBO);
        
        //Connect the Objects
        poBO.setPrintFormatID(ptBO.getId());
        poBO.setPhotoSetID(psBO.getId());
        poBO.setQty(qty);
        poBO.setDescription(descr);
        txBO.setRevSrc(poBO);
        
        //Calculates the RS Price
        poBO.setPrice(unitPrice * qty);
        
        //Save Everything
        
        poDAO.save(poBO);
        
        //Return
        return poBO;
    }
    
    public ArrayList<BusinessObject> listPhotoSets() throws DataException, SQLException, ConnectionPoolException {
        PhotoSetDAO psDAO               =      new PhotoSetDAO();
        return  psDAO.readAll();
    }
    
    public void makePhotoSet(String description, int numberinset) throws Exception {
        PhotoSetDAO psDAO = (PhotoSetDAO)PhotoSetDAO.getInstance();
        PhotoSetBO psBO = (PhotoSetBO)psDAO.create();
        psBO.setNuminSet(numberinset);
        psBO.setDescription(description);
        psDAO.save(psBO);
        
    }
    
    public void editPhotoSet(String guid, String description, int numberinset) throws Exception {
        PhotoSetDAO psDAO = (PhotoSetDAO)PhotoSetDAO.getInstance();
        PhotoSetBO psBO = (PhotoSetBO)psDAO.read(guid);
        psBO.setNuminSet(numberinset);
        psBO.setDescription(description);
        psDAO.save(psBO);
        
    }
    
    
    public RevenueSourceBO getPrintOrder(String id) throws SQLException, DataException, ConnectionPoolException {
        PrintOrderDAO poDAO = PrintOrderDAO.getInstance();
        return poDAO.read(id);
    }
    
    public void markReady(String id) throws DataException, ConnectionPoolException, SQLException {
        
        PrintOrderDAO poDAO = (PrintOrderDAO)PrintOrderDAO.getInstance();
        PrintOrderBO poBO =  (PrintOrderBO)poDAO.read(id);
        poBO.setStatus("Ready");
        poDAO.save(poBO);
    }
    
    public ArrayList<RevenueSourceBO> batchAdd(int qty, String descr, String PhotoSetID, String size, String printType, String photoType, double unitPrice, int batchamount) throws Exception {
        ArrayList<RevenueSourceBO>  txLnz = new ArrayList<RevenueSourceBO>() ;
        int i  = 0;
        while (i<batchamount) {
            txLnz.add(addPrint( qty,  descr, PhotoSetID,  size,  printType,  photoType,  unitPrice));
            i++;
        }
        return txLnz;
    }
    
    public PrintFormatBO getFormat(String id) throws SQLException, DataException, ConnectionPoolException {
        PrintFormatDAO pfDAO = (PrintFormatDAO) new PrintFormatDAO();
        return (PrintFormatBO)pfDAO.read(id);
    }
    
    public PhotoSetBO getSet(String id) throws SQLException, DataException, ConnectionPoolException {
        PhotoSetDAO psDAO = (PhotoSetDAO)PhotoSetDAO.getInstance();
        
        return (PhotoSetBO)psDAO.read(id);
    }
    
    public String[] getPSDesc() throws DataException, SQLException, ConnectionPoolException {
        ArrayList<BusinessObject> psList = this.listPhotoSets();
        int size = psList.size();
        int i = 0;
        String[] psArray = new String[size];
        while (i < size) {
            PhotoSetBO psBO = (PhotoSetBO)psList.get(i);
            psArray[i] = psBO.getDescription()+" ("+psBO.getNuminSet()+")";
            i++;
        }
   return psArray;
    }
}